package com.mxx.bu_yang.adapter;

import android.app.Activity;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseDelegateMultiAdapter;
import com.chad.library.adapter.base.delegate.BaseMultiTypeDelegate;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.mxx.bu_yang.R;
import com.mxx.bu_yang.response.LocationHintResponse;

import org.jetbrains.annotations.NotNull;

import java.util.List;


/**
 * 作者：刘江飞
 * 时间：2020/10/14  14:42
 * 邮箱：642371742Z@qq.com
 * 描述：货品批次
 **/
public class LocationHintAdapter extends BaseDelegateMultiAdapter<LocationHintResponse, BaseViewHolder> {
    private Activity activity;

    public LocationHintAdapter(Activity activity) {
        super();
        this.activity = activity;
        // 第一步，设置代理
        setMultiTypeDelegate(new BaseMultiTypeDelegate<LocationHintResponse>() {
            @Override
            public int getItemType(@NotNull List<? extends LocationHintResponse> data, int position) {
                // 根据数据，自己判断应该返回的类型
                LocationHintResponse bean = data.get(position);
                int itemType = bean.item_type;
                switch (itemType) {
                    case 0:
                        return LocationHintResponse.TYPE_HEADER;
                    case 1:
                        return LocationHintResponse.TYPE_CONTENT;
                    default:
                        break;
                }
                return bean.item_type;
            }
        });
        // 第二部，绑定 item 类型
        if (getMultiTypeDelegate() != null) {
            getMultiTypeDelegate()
                    .addItemType(LocationHintResponse.TYPE_HEADER, R.layout.adapter_location_hint)
                    .addItemType(LocationHintResponse.TYPE_CONTENT, R.layout.adapter_location_hint);
        }
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, LocationHintResponse response) {
        TextView tv_name = baseViewHolder.getView(R.id.tv_name);
        switch (baseViewHolder.getItemViewType()) {

            case LocationHintResponse.TYPE_HEADER:
                if (response.order_num != null) {
                    tv_name.setText(response.order_num);
                }
                break;
            case LocationHintResponse.TYPE_CONTENT:
                String warehouse_name = response.warehouse_name;
                String start = warehouse_name + " -- 占";
                String actual = response.actual;
                String value = start + actual;
                SpannableString spannableString = new SpannableString(value);
                spannableString.setSpan(new ForegroundColorSpan(activity.getResources().getColor(R.color.color_1a77d6))
                        , start.length(), value.length()
                        , Spanned.SPAN_INCLUSIVE_EXCLUSIVE
                );
                tv_name.setText(spannableString);
                break;
        }
    }
}
